package com.inossem.warehousestorage_inossem.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.system.SettingAddressActivity;
import com.inossem.warehousestorage_inossem.application.WarehouseStorageApplication;
import com.inossem.warehousestorage_inossem.bean.LoginBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.LoginRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

public class LoginActivity extends BaseActivity {
    private static final String SUCCESS = "1";//成功

    private TextView title, versionName;

    private EditText userName, password;

    private Button submit;

    private boolean flag = false;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        initView(LoginActivity.this);
        initData(LoginActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        versionName = (TextView) activity.findViewById(R.id.versionName);
        userName = (EditText) this.findViewById(R.id.userName);
        password = (EditText) this.findViewById(R.id.password);
        submit = (Button) this.findViewById(R.id.submit);
    }

    private void initData(Activity activity) {
        title.setText("企业智能仓储管理系统");
        versionName.setText("Ver. " + Utils.getVersionName(activity));
        ActivityUtils.initRightFunction(activity, getRightFunction(activity), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                startActivity(new Intent(activity, SettingAddressActivity.class));
//                startActivity(new Intent(activity, TestActivity.class));
            }
        });
        userName.setText("");
        password.setText("");
        userName.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            if (hasFocus) {
                userName.setText("");
            }
        });
        password.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            if (hasFocus) {
                password.setText("");
            }
        });
        submit.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String result = check(activity);
                if (result == null) {
                    LoginRequest.request(activity, userName.getText().toString(), password.getText().toString(), new GsonBeanListener<LoginBean>() {
                        @Override
                        public void onSuccess(LoginBean bean) {
                            if (SUCCESS.equals(bean.getData().getCode())) {
                                flag = true;
                                String token = bean.getData().getToken();
                                SPUtils.saveToken(activity, token);
                                SPUtils.saveLoginFlag(activity, true);
                                SPUtils.saveName(activity, bean.getData().getRealName());
                                Intent intent = new Intent();
                                intent.setClass(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            } else {
                                if (!TextUtils.isEmpty(bean.getData().getMsg())) {
                                    Utils.middleToast(activity, bean.getData().getMsg());
                                }
                            }
                        }
                    });
                } else {
                    Utils.middleToast(activity, result);
                }
            }
        });
    }

    private View getRightFunction(Context context) {
        View rightFunctionView = LayoutInflater.from(context).inflate(R.layout.include_right_function, null);
        ImageView rightFunction = (ImageView) rightFunctionView.findViewById(R.id.rightFunction);
        rightFunction.setImageResource(R.drawable.ico_setting);
        return rightFunctionView;
    }

    private String check(Context context) {
        if (TextUtils.isEmpty(userName.getText().toString())) {
            return "用户名不可以为空";
        }
        if (TextUtils.isEmpty(password.getText().toString())) {
            return "密码不可以为空";
        }
        return null;
    }

    @Override
    public void onBackPressed() {
        if (!flag) {
            LogUtils.i("关闭所有页面");
            WarehouseStorageApplication.getInstance().removeAllActivity();
        }
        super.onBackPressed();
    }

}
